﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyBank
{
    public class CurrentAccount:Account
    {
        public static new int account_ID;  // 当前系统的登录的账户ID
        public static new string account_Name;  // 当前系统的登录的账户姓名
        public static string account_Tel;  // 当前系统的登录的账户姓名
        public static int isAdmin;
        public static bool isLogin = false;  // 当前系统的登陆状态
        public static List<Card> all_Cards = new List<Card>();  // 账户下所有的银行卡
        public List<Bank> Banks = new List<Bank>();  // 所有的银行
        static public SqLiteHelper sql = new SqLiteHelper("data source = Bank.db");

        public bool login_Account_by_ID(int id, string password) // 根据id和密码载入账户信息
        {
            
            SQLiteDataReader reader = sql.ReadFullTable("Account");

            int temp_id;
            string temp_password;
            while (reader.Read())
            {
                temp_id = reader.GetInt32(reader.GetOrdinal("account_ID"));
                temp_password = reader.GetString(reader.GetOrdinal("account_Password"));

                if (temp_id == id && temp_password == password)
                {
                    CurrentAccount.account_ID = temp_id;
                    CurrentAccount.account_Name = reader.GetString(reader.GetOrdinal("account_Name"));
                    CurrentAccount.isAdmin = reader.GetInt32(reader.GetOrdinal("is_Admin"));
                    CurrentAccount.account_Tel = reader.GetString(reader.GetOrdinal("account_Tel"));
                    this.account_Tempbalance = reader.GetDouble(reader.GetOrdinal("account_Tempbalance"));
                    
                    reader.Close();
                    load_Cards();
                    load_Banks();
                    isLogin = true;
                    return true; 
                }
            }
            //sql.CloseConnection();  // 关闭数据库
            return false;
        }

        public void load_temp() 
        {
            SQLiteDataReader reader = sql.ReadFullTable("Account");
            int temp_id;
            while (reader.Read())
            {
                temp_id = reader.GetInt32(reader.GetOrdinal("account_ID"));
                if (temp_id == CurrentAccount.account_ID)
                {
                    this.account_Tempbalance = reader.GetDouble(reader.GetOrdinal("account_Tempbalance"));
                }
            }
            reader.Close();
        }

        public void load_Cards()
        {
            
            all_Cards.Clear();
            // 载入账户银行卡
            SQLiteDataReader reader2 = sql.ReadFullTable("AllCards");
            int temp_account_id;
            while (reader2.Read())
            {
                
                temp_account_id = reader2.GetInt32(reader2.GetOrdinal("account_ID"));

                if (CurrentAccount.account_ID == temp_account_id)
                {
                    int Card_ID = reader2.GetInt32(reader2.GetOrdinal("Card_ID"));
                    int Card_num = reader2.GetInt32(reader2.GetOrdinal("Card_num"));
                    double Card_Balance = reader2.GetInt32(reader2.GetOrdinal("Card_Balance"));
                    string Account_Name = reader2.GetString(reader2.GetOrdinal("Account_Name"));
                    string Card_Tel = reader2.GetString(reader2.GetOrdinal("Card_Tel"));
                    string Card_Name = reader2.GetString(reader2.GetOrdinal("Card_Name"));
                    all_Cards.Add(new Card(Card_ID, Card_num, Account_Name, Card_Balance, Card_Tel,Card_Name));
                }
            }

            


        }

        public void load_Banks()
        {
            // 载入银行列表
            Banks.Clear();
            SQLiteDataReader Bank_reader = sql.ReadFullTable("Bank");
            while (Bank_reader.Read())
            {
                int temp_bank_id = Bank_reader.GetInt32(Bank_reader.GetOrdinal("Bank_ID"));
                string temp_bank_name = Bank_reader.GetString(Bank_reader.GetOrdinal("Bank_Name"));
                Banks.Add(new Bank(temp_bank_id, temp_bank_name));
            }
        }

        public void add_Card(double preBalance,int Card_ID,int Bank_ID,string Card_Name)
        {
            SQLiteDataReader reader = sql.ReadFullTable("AllCards");
            int Card_num = 0;
            while (reader.Read())
            {
                Card_num = reader.GetInt32(reader.GetOrdinal("Card_num"));
            }
            // 载入银行列表
            CurrentAccount.sql.InsertValues("AllCards", new string[] { (Card_num + 1).ToString(), Card_ID.ToString(), "储蓄卡", Bank_ID.ToString(), CurrentAccount.account_ID.ToString(),CurrentAccount.account_Name,preBalance.ToString(),CurrentAccount.account_Tel, Card_Name});
            Banks.Clear();
            SQLiteDataReader Bank_reader = sql.ReadFullTable("Bank");
            while (Bank_reader.Read())
            {
                int temp_bank_id = Bank_reader.GetInt32(Bank_reader.GetOrdinal("Bank_ID"));
                string temp_bank_name = Bank_reader.GetString(Bank_reader.GetOrdinal("Bank_Name"));
                Banks.Add(new Bank(temp_bank_id, temp_bank_name));
            }
        }



        // 检查用户是否存在
        public bool checkID(string transID)
        {
            if (transID == CurrentAccount.account_ID.ToString()) { 
                MessageBox.Show("自己不能给自己转钱！！");
                return false;
            }
            
            SQLiteDataReader reader = sql.ReadFullTable("Account");
            int temp_id;
            while (reader.Read())
            {
                temp_id = reader.GetInt32(reader.GetOrdinal("account_ID"));

                if (temp_id == CurrentAccount.account_ID)
                {
                    reader.Close();
                    return true;
                }
            }
            MessageBox.Show("账户不存在！！！");
            return false;
        }


        // 从临时钱包取出
        public bool WithDrawMoney(double money)
        {
            if (money > this.account_Tempbalance) return false;  //取出金额要小于等于余额
            this.account_Tempbalance -= money;
            SQLiteDataReader reader = CurrentAccount.sql.ReadFullTable("Account");
            while (reader.Read())
            {
                if(CurrentAccount.account_ID == reader.GetDouble(reader.GetOrdinal("account_ID")))
                {
                    double oldMoney = reader.GetDouble(reader.GetOrdinal("account_Tempbalance"));
                    CurrentAccount.sql.UpdateValues("Account", new string[] { "account_Tempbalance" }, new string[] { (oldMoney - money).ToString() }, "account_ID", CurrentAccount.account_ID.ToString());
                }
                return true;
            }
            return true;
        }

        // 存入临时钱包
        public bool SaveMoney(double money)
        {
            if (this.account_Tempbalance < 0) return false;  // 卫语句（存入金额不能为负）
            this.account_Tempbalance += money;

            SQLiteDataReader reader = CurrentAccount.sql.ReadFullTable("Account");
            while (reader.Read())
            {
                if (CurrentAccount.account_ID == reader.GetDouble(reader.GetOrdinal("account_ID")))
                {
                    double oldMoney = reader.GetDouble(reader.GetOrdinal("account_Tempbalance"));
                    CurrentAccount.sql.UpdateValues("Account", new string[] { "account_Tempbalance" }, new string[] { (oldMoney + money).ToString() }, "account_ID", CurrentAccount.account_ID.ToString());

                }
                    return true;
            }

            return false;


        }

    }


}
